// pages/andon/index/index.js
// import * as echarts from "../../../packageB/components/echarts/echarts";
import * as echarts from "../../../components/echarts/echarts";
// import * as echarts from "../../../components/echarts/echarts"
const mybehavior = require("../../../../behaviors/requests")

function drawChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  });
  canvas.setChart(chart);
  wx.request({
    url: "https://logisticsengineering.gree.com/andon_system/get_all_times",
    method: "get",
    data: {
    },
    success(res) {
      let data = res.data.data;
      let factory_data = [];
      let factories = [];
      data.forEach(element => {
        factory_data.push(element.all_times)
        factories.push(element.factory)
      });
      console.log(factory_data, factories)
      let option = {
        title: {
          text: '班组叫料次数统计',
          right: "center",
          top: 10,
          textStyle: { color: "black", fontSize: 20 }
        },
        color: ['#3398DB'],
        tooltip: {
          trigger: 'item',
          axisPointer: {            // 坐标轴指示器，坐标轴触发有效
            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          show: true,
          left: '3%',
          right: '4%',
          bottom: 0,
          containLabel: true,
        },
        xAxis: [
          {
            type: 'category',
            data: factories,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              rotate: 45, // 设置旋转角度为45度
            },
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            type: 'bar',
            barWidth: '60%',
            data: factory_data
          }
        ]
      };
      chart.setOption(option);
    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    ec: {
      onInit: drawChart
    },
    taskId: "",
    list: [],
    factory_index: -1,
    factory_list: "",
    factory_data: "",
    factory_name: "",
    value1: 0,
    value2: 'a'
  },
  getTotalData() {
    const url = "http://logisticsengineering.gree.com/andon_system/get_all_times";
    const that = this
    this.get(url).then((res) => {
      console.log(res.data)
      that.setData({
        list: res.data
      })
    }).catch((err) => {
      console.log(err)
    })
  },
  setFactory(event) {
    const index = event.currentTarget.dataset['index'];
    this.setData({
      factory_index: index,
      factory_list: this.data.list[index].inner_data,
      factory_name: this.data.list[index].factory
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTotalData();
    let taskId = setInterval(this.getTotalData, 30000);
    this.setData({
      taskId: taskId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.taskId);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})